package com.atguigu.gmall.oms.lister;

import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Component;


import java.io.IOException;

@Component
public class OrderLister {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "ORDER-UPDATE"),
            exchange = @Exchange(value = "ORDER_EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true")
            ,key = "order.failer"))
    public void orderUpdate(String orderToken, Channel channel, Message message) throws IOException {
        //判读订单号是否为空
        if(StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        try {
            //修改库存的订单状态
            orderMapper.updateStatus(orderToken,0,5);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //拒签basicReject
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

    @RabbitListener(queues = "ORDER-CLOSE-QUEUE")
    public void colseOrder(String orderToken,Channel channel,Message message) throws IOException {
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        //修改订单装 为已关单
        if (orderMapper.updateStatus(orderToken,0,4)==1){
           //发送消息去解锁库存
            rabbitTemplate.convertAndSend("ORDER_EXCHANGE","stock.unlock",orderToken);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

    /*
    修改订单为已支付状态
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "ORDER-UPDATEPAY"),
            exchange = @Exchange(value = "ORDER_EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true")
            ,key = "order.pay"))
    public void orderUpdatePay(String orderToken, Channel channel, Message message) throws IOException {
        //判读订单号是否为空
        if(StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        try {
            //修改库存的订单状态
            orderMapper.updateStatus(orderToken,0,4);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //拒签basicReject
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}
